﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using MockupDesigner.Editors.TextEditors.Lists;
using MockupDesigner.AppItems;
using System.ComponentModel;

namespace MockupDesigner.Items
{
    [TreeViewEditor]
    public class TreeViewItem : ResizableItem
    {
        public static readonly DependencyProperty ItemsSourceProperty =
           DependencyProperty.Register("ItemsSource", typeof(System.Windows.Controls.TreeViewItem[]), typeof(TreeViewItem));

        public System.Windows.Controls.TreeViewItem[] ItemsSource
        {
            get
            {
                return (System.Windows.Controls.TreeViewItem[])GetValue(ItemsSourceProperty);
            }
            set
            {
                if (AreItemsEqual(value))
                    return;
                this.autochanging = true;
                OnPropertyChanging("ItemsSource", ItemsSource, value);
                SetValue(ItemsSourceProperty, value);
                if (this.Parent is DesignerItem)
                {
                    base.Height = NumberOfItems * 25;
                    base.Width = this.Depth > 0 ? 50 * (this.Depth + 1) : 50;
                }
                OnPropertyChanged("ItemsSource");
                this.autochanging = false;
            }
        }

        // Explicitly hidden property : non serializable
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int Depth { get; set; }

        // Explicitly hidden property : non serializable
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int NumberOfItems { get; set; }

        private bool AreItemsEqual(System.Windows.Controls.TreeViewItem[] value)
        {
            if (ItemsSource == null && value == null)
                return true;
            if (ItemsSource == null && value != null)
                return false;
            if (ItemsSource != null && value == null)
                return false;
            if (value.Count() != ItemsSource.Count())
                return false;
            for (int i = 0; i < value.Count(); i++)
            {
                if (!AreSubItemsEqual(value[i], ItemsSource[i]))
                    return false;
            }
            return true;
        }

        private bool AreSubItemsEqual(System.Windows.Controls.TreeViewItem item1, System.Windows.Controls.TreeViewItem item2)
        {
            if (item1.Items.Count != item2.Items.Count)
                return false;
            if (item1.Header != item2.Header)
                return false;
            for (int i = 0; i < item1.Items.Count; i++)
            {
                if (!AreSubItemsEqual(item1.Items[i] as System.Windows.Controls.TreeViewItem, item2.Items[i] as System.Windows.Controls.TreeViewItem))
                    return false;
            }
            return true;
        }

        public TreeViewItem()
        {
            System.Windows.Controls.TreeViewItem[] array = new System.Windows.Controls.TreeViewItem[1];
            System.Windows.Controls.TreeViewItem item = new System.Windows.Controls.TreeViewItem();
            item.Header = "Item1";
            item.IsExpanded = true;
            array[0] = item;
            item.Items.Add(new System.Windows.Controls.TreeViewItem() { Header = "Item2" });
            item.Items.Add(new System.Windows.Controls.TreeViewItem() { Header = "Item3" });
            item.Items.Add(new System.Windows.Controls.TreeViewItem() { Header = "Item4" });
            this.ItemsSource = array;
        }


    }
}
